/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-18
 * Time: 下午1:06
 * To change this template use File | Settings | File Templates.
 */
Ext.define('Airways.controller.system.operation.OperationManageController', {
    extend:'Ext.app.Controller',
    stores:['system.operation.OperationManageStore'],
    models:['system.operation.OperationManageModel'],
    views:['system.operation.OperationManageView'],
    refs:[
        {ref:'operationManageView',selector:'operationManageView'},
        {ref:'popUpWindow',selector:'popUpWindow'},
        {ref:'popUpPanel',selector:'popUpPanel'},
        {ref:'leftPanel',selector:'leftPanel'},
        {ref:'rightPanel',selector:'rightPanel'},
        {ref:'addFlatMenuAction',selector:'addFlatMenuAction'},
        {ref:'addChildMenuAction',selector:'addChildMenuAction'},
        {ref:'editCurrentMenuAction',selector:'editCurrentMenuAction'},
        {ref:'deleteCurrentMenuAction',selector:'deleteCurrentMenuAction'}
    ],
    init:function() {
        var selectNode = null;
        var currentNode = null;
        var _rightPanel = null;
        var _leftPanel = null;
        var _popUpWindow = null;
        var _contextMenu = null;
        var opType = "";
        var View ;
        var _object  = this;
        this.control({
            'operationManageView':{
                afterrender:function(){

                }
            },
            'addFlatMenuAction':{
                click:function (b){
                    addFlatMenuActionEvent(this,b);
                }
            },
            'addChildMenuAction':{
                click:function (b){
                    addChildMenuActionEvent(this,b);
                }
            },
            'editCurrentMenuAction':{
                click:function (b){
                    editCurrentMenuActionEvent(this,b);
                }
            },
            'deleteCurrentMenuAction':{
                click:function (b){
                    deleteCurrentMenuActionEvent(this,b);
                }
            },
            'popUpWindow button[name=saveButton]':{
                click:function(){
                    if(!_popUpWindow.down('form').getForm().isValid()){
                        Ext.MessageBox.show({title:'提示',msg:'按照提示填写数据!',icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                        return;
                    }
                    var url = basePath+'system/operation/saveOperation';
                    var params = _popUpWindow.down('form').getForm().getFieldValues();
                    params.opType = opType;
                    //调用ajax 发送数据到后台
                    callAjaxLocal(url,params,handleResult);
                }
            },
            'popUpWindow button[name=cancelButton]':{
                click:function(){
                    closeEvent(this);
                }
            },
            'popUpWindow':{
                close:function(){
                    //触发取消按钮的 单击 事件
                    closeEvent(this);
                }
            },
            'leftPanel treeview':{
                changeIndexDrop:function(obj){
                    this.indexObj = Ext.apply(this.indexObj,obj)
                    var url = basePath+'system/operation/changeOperationIndex';
//                  调用ajax 发送数据到后台
                  callAjaxLocal(url,this.indexObj,handleChangeIndexResult);
                },
                changeIndexBeforeDrop:function(obj){
                    this.indexObj  = obj;
                }
            },
            'leftPanel':{
                itemclick:function(view,record,item,index,evt,options){
                    selectNode = record;
                    currentNode = null;
                    _rightPanel = this.getRightPanel();
                    _rightPanel.loadRecord(record);
                },
                itemcontextmenu:function(view,record,item,index,evt,options){
                    View = view;
                    evt.stopEvent();
                    selectNode = record;
                    currentNode = null;
                    _rightPanel = this.getRightPanel();
                    _rightPanel.loadRecord(record);
                    if(_contextMenu == undefined){
                        var _addFlatMenuAction = Ext.create('Ext.Action', {
                            iconCls: 'add',
                            text: '添加平级菜单',
                            disabled: false,
                            handler: function(widget, event) {
                                addFlatMenuActionEvent(_object,event.getTarget());
                            }
                        });
                        var _addChildMenuAction = Ext.create('Ext.Action', {
                            iconCls: 'add',
                            text: '添加子菜单',
                            disabled: false,
                            handler: function(widget, event) {
                                addChildMenuActionEvent(_object,event.getTarget());
                            }
                        });
                        var _editCurrentMenuAction = Ext.create('Ext.Action', {
                            iconCls: 'edit',
                            text: '编辑当前菜单',
                            disabled: false,
                            handler: function(widget, event) {
                                editCurrentMenuActionEvent(_object,event.getTarget());
                            }
                        });
                        var _deleteCurrentMenuAction = Ext.create('Ext.Action', {
                            iconCls: 'delete',
                            text: '删除当前菜单',
                            disabled: false,
                            handler: function(widget, event) {
                                deleteCurrentMenuActionEvent(_object,event.getTarget());
                            }
                        });
                        _contextMenu = Ext.create('Ext.menu.Menu', {
                            items: [
                                _addFlatMenuAction,
                                _addChildMenuAction,
                                _editCurrentMenuAction,
                                _deleteCurrentMenuAction
                            ]
                        });
                    }
                    _contextMenu.showAt(evt.getXY());
                    return false;
                },
                itemexpand:function(node,eOpts){
                    node.set({iconCls:node.data.expandedCls});
                },
                itemcollapse:function(node,eOpts){
                    node.set({iconCls:node.data.collapsedCls});
                },
                load:function(){
                    _leftPanel = this.getLeftPanel();
                    _leftPanel.expandAll();
                }
            }
        });
        function handleResult(result){
            if(result.success){
                Ext.MessageBox.show({title:'提示',msg:'操作已成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK});
            }
            if(currentNode !=null){
                currentNode.set(result.data);
                currentNode.updateInfo();
                _rightPanel.loadRecord(currentNode);
            }else{
                selectNode.set(result.data);
                selectNode.updateInfo();
                _rightPanel.loadRecord(selectNode);
            }
            _popUpWindow.hide();
        }
        function handleChangeIndexResult(result){
            if(result.success){
                Ext.DropTip.msg('提示','拖动排序成功!')
                _leftPanel.getStore().load();
            }else{
                Ext.DropTip.msg('提示','拖动排序失败!')
            }
        }
        function handleDeleteResult(result){
            currentNode.remove();
            if(!selectNode.hasChildNodes()){
                selectNode.set({
                    leaf:true
                });
                selectNode.updateInfo();
            }
        }
        function addFlatMenuActionEvent(object,b){
            _leftPanel = object.getLeftPanel();
            _popUpWindow = object.getPopUpWindow();
            if(_popUpWindow == undefined){
                _popUpWindow = Ext.create('Airways.view.system.operation.PopUpWindow',{
                    animateTarget:_leftPanel
                });
            }
            _popUpWindow.down('form').getForm().reset();
            if(selectNode != null){
                opType = "add";
                _popUpWindow.setTitle('添加平级菜单');
                _popUpWindow.show(b);
                currentNode = selectNode.parentNode.insertBefore({
                    text: '新菜单',
                    menuName: '新菜单',
                    menuLevel:selectNode.parentNode.data.menuLevel+1,
                    menuIndex:selectNode.parentNode.childNodes.length,
                    status:1,
                    leaf: true
                });
                _popUpWindow.down('form').getForm().loadRecord(currentNode);
            }else{
                Ext.MessageBox.show({title:'提示',msg:'请先选中菜单!',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.WARNING});
                return;
            }
        }
        function addChildMenuActionEvent(object,b){
            _leftPanel = object.getLeftPanel();
            _popUpWindow = object.getPopUpWindow();
            if(_popUpWindow == undefined){
                _popUpWindow = Ext.create('Airways.view.system.operation.PopUpWindow',{
                    animateTarget:_leftPanel
                });
            }
            _popUpWindow.down('form').getForm().reset();
            if(selectNode != null){
                opType = "add";
                _popUpWindow.setTitle('添加子级菜单');
                _popUpWindow.show(b);
                selectNode.data.leaf = false;
                currentNode = selectNode.appendChild({
                    text: '新菜单',
                    menuName: '新菜单',
                    menuLevel:selectNode.data.menuLevel+1,
                    menuIndex:selectNode.childNodes.length,
                    status:1,
                    leaf: true
                });
                currentNode.updateInfo();
                _popUpWindow.down('form').getForm().loadRecord(currentNode);
                selectNode.expand();
            }else{
                Ext.MessageBox.show({title:'提示',msg:'请先选中菜单!',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.WARNING});
                return;
            }
        }
        function editCurrentMenuActionEvent(object,b){
            _leftPanel = object.getLeftPanel();
            _popUpWindow = object.getPopUpWindow();
            if(_popUpWindow == undefined){
                _popUpWindow = Ext.create('Airways.view.system.operation.PopUpWindow',{
                    animateTarget:_leftPanel
                });
            }
            _popUpWindow.down('form').getForm().reset();
            if(selectNode != null){
                opType = "edit";
                _popUpWindow.setTitle('编辑菜单');
                _popUpWindow.down('form').getForm().loadRecord(selectNode);
                _popUpWindow.show(b);
            }else{
                Ext.MessageBox.show({title:'提示',msg:'请先选中菜单!',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.WARNING});
                return;
            }
        }
        function deleteCurrentMenuActionEvent(object,b){
            if(selectNode != null){
                if(selectNode.hasChildNodes()){
                    Ext.MessageBox.show({title:'操作错误',msg:'当前选中菜单含有子菜单项，请先删除子菜单!',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                    return;
                }
                opType = "delete";
                Ext.MessageBox.confirm("提示","确定要删除当前菜单吗",function (e){
                    if( e == "yes"){
                        currentNode = selectNode;
                        selectNode = selectNode.parentNode;
                        var url = basePath+'system/operation/deleteOperation';
                        var params = new Object();
                        params.opType = opType;
                        params.operationId = currentNode.get('operationId');
                        params.version = currentNode.get('version');
                        var isTooltip = false;
                        //调用ajax 发送数据到后台
                        callAjax(url,params,handleDeleteResult);
                    }
                });
            }else{
                Ext.MessageBox.show({title:'提示',msg:'请先选中菜单!',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.WARNING});
                return;
            }
        }
        function closeEvent(object){
            if(currentNode !=null){
                currentNode.remove();
                if(!selectNode.hasChildNodes()){
                    selectNode.set({
                        leaf:true
                    });
                    selectNode.updateInfo();
                }
            }
            _popUpWindow = object.getPopUpWindow();
            _popUpWindow.hide();
        }
    }
});
